<?php
namespace App\Modules\Relationship\Models;


use App\Http\Services\CommonServices;
use App\Modules\Relationship\Interfaces\ModelInterface;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\Base;

class SweetUserTitleModel extends Base {

    protected $table = "rs_sweet_user_title";
    protected $titleTable = "rs_sweet_title";
    protected $fields = ['id', 'sweet_id as sweetId', 'user_id as userId', 'title_id as titleId', 'rs_code as rsCode'];
    protected $tablePrefix = '';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
        $this->tablePrefix = env('DB_PREFIX');
    }

    public function getTitles ($sweetId) : array {
        $tableName = $this->tablePrefix . $this->table;
        $titleTableName = $this->tablePrefix . $this->titleTable;

        $sql = "SELECT user_id as userId, title_id as titleId, t.title as titleName 
        FROM {$tableName} AS u 
        LEFT JOIN {$titleTableName} AS t 
        ON u.title_id = t.id
        WHERE u.sweet_id = ?
        AND u.is_deleted = 0
        AND t.is_deleted = 0
        LIMIT 2
        ";

        $rows = DB::select($sql, array($sweetId));
        return $rows;
    }
}

